from django.utils.deprecation import MiddlewareMixin
from django.contrib.auth.models import AnonymousUser
from django.shortcuts import redirect
import datetime
from application.scheduler import scheduler

time = datetime.datetime.now().timestamp()

@scheduler.scheduled_job(trigger="interval", seconds=60*60*1)
def up_time():
    global time
    time = datetime.datetime.now().timestamp()

class MyMiddleware(MiddlewareMixin):
    def process_request(self,request):
        if (request.session.get_expiry_date().timestamp() - time) < 60*60*3:
            request.session.set_expiry = (request.session.get_expiry_date()+datetime.timedelta(hours=6))
            request.session.save()

        if isinstance(request.user, AnonymousUser):
            if(request.path.startswith('/virt/display/info/') or request.path.startswith('/get_img/')):
                return
            if(request.path != "/login"):
                return redirect("/login")
            
    # def process_view(self,request,callback,callback_args,callback_kwargs):
    #     pass
        
    def  process_response(self,request,response):
        """
            print("请求响应前执行")
        """
        return response
    
